<?php
use yii\helpers\Html;
use yii\helpers\ArrayHelper;
use yii\bootstrap\ActiveForm;
use yii\widgets\LinkPager;
use yii\grid\GridView;
use yii\helpers\Url;
$this->title = '角色管理';
?>
<div class="container">
	<div class="row">
		<?php echo $this->render('_leftnav') ?>
		<div class="col-md-10">
			<div class="page-header clearfix">
				<h1 class="pull-left">角色管理</h1>
				<div class="pull-right">
					<a class="btn btn-success btn-sm" data-toggle="modal" data-target="#addRole">添加角色</a>
				</div>
			</div>
			<?php echo $this->render('_adminnav') ?>
			<?php 
				$form = ActiveForm::begin([
					'options' => ['class' => 'form-inline well well-sm',
					'id'=>'comment-search-form'],
					'action'=>Url::to(['user/role']),
					'method'=>'post',
					'fieldConfig' => [
						'template' => '<div class="form-group"><div class="col-md-8 controls"  style="padding-top: 9px">{input}{error}</div></div>'
					], 
				]);
			?>
				<div class="form-group">
					<?php 
						$Role=['' => '--关键字类型--','rname' => '角色名称','aremark' => '角色信息'];
					?>
					<?= Html::dropDownList('role', $search['role'], ArrayHelper::map($Role,'id', 'role'), ['class' => 'form-control role']);?>
				</div>

				<div class="form-group">
					<?= Html::input('text', 'role_value', $search['value'], ['class' => 'form-control','id' => 'role_value','placeholder' => '关键字']) ?>
				</div>

				<?= Html::submitButton('搜索', ['class' => 'btn btn-primary','id'=>'role-search-btn']) ?>
			<?php ActiveForm::end(); ?> 
			<table id="user-table" class="table table-striped table-hover" data-search-form="#user-search-form">
				<thead>
					<tr>
						<th>角色名称</th>
						<th>状态</th>
						<th>角色信息</th>
						<th>操作</th>
					</tr>
				</thead>
				<tbody>
					<?php if($data){?>
					<?php foreach($data as $key => $val){?>
						<tr id="user-table-tr-14">
							<td>
								<strong>
									<?= Html::encode($val['role_name']) ?>
								</strong>
							</td>
							<td>
								<?php if($val['role_status'] == "1"){?>
									<span id="<?php echo Html::encode($val['role_id']) ?>"><label class="label label-primary">开启</label></span>
								<?php }else{?>
									<span id="<?php echo Html::encode($val['role_id']) ?>"><label class="label label-danger">禁用</label></span>
								<?php }?>
							</td>
							<td>
								<span class="text-muted text-sm">
									<?php if($val['role_remark']){?>
										<?php echo Html::encode($val['role_remark']) ?>
									<?php }else{
										echo "--";
									}?>
								</span>
							</td>
							<td>
								<div class="btn-group">
									<a href="#modal" data-toggle="modal" data-target="#RoleInfo" class="btn btn-default btn-sm RoleInfo" type="<?= Html::encode($val['role_id'])?>">查看</a>
									<a href="#" type="button" class="btn btn-default btn-sm dropdown-toggle" data-toggle="dropdown">
										<span class="caret"></span>
									</a>
									<ul class="dropdown-menu">
										<li>
											<a class="promote-user Auth" href="javascript:" data-toggle="modal" data-target="#Auth" type="<?= Html::encode($val['role_id'])?>">分配权限</a>
											<a class="promote-user Save" href="javascript:" data-toggle="modal" data-target="#RoleUpdate" type="<?= Html::encode($val['role_id'])?>">编辑</a>
											<a class="promote-user Delete" href="javascript:" type="<?= Html::encode($val['role_id'])?>">删除</a>
											<?php if($val['role_status'] == "1"){
											?>
												<a href="javascript:;"  class="promote-user Off" type="<?= Html::encode($val['role_id'])?>">禁用</a>
											<?php }else{ ?>
												<a href="javascript:;"  class="promote-user On" type="<?= Html::encode($val['role_id'])?>">启用</a>
											<?php }?>
										</li>
									</ul>
								</div>
							</td>
						</tr>
					<?php }}else{?>
						<tr>
							<td colspan="20">
								<div class="empty">暂无角色记录</div>
							</td>
						</tr>
					<?php }?>
				</tbody>
			</table>
			<div align="right">
				<?php echo LinkPager::widget([
					'pagination' => $pagination,
					'prevPageLabel'=>'上一页',
					'nextPageLabel'=>'下一页',
				]);?>
			</div>
		</div>
	</div>
</div>
<div id="addRole" class="modal fade">
	<div class="modal-dialog">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
				<h4 class="modal-title">添加分类</h4>
			</div>
			<div class="modal-body">
				<div id="type-creater-widget">
					<?php 
						$form = ActiveForm::begin([
							'options' => ['class' => 'form-horizontal',
							'id'=>'type-form'],
							'action'=>Url::to(['user/rolecreate']),
							'method'=>'post',
							'fieldConfig' => [
								'template' => '<div class="form-group"><center><label class="col-md-2 control-label" for="type-name-field">{label}</label></center><div class="col-md-8 controls">{input}{error}</div></div>'
							], 
						]);
					?>
					<?= $form->field($model, 'role_name',['inputOptions'=>['placeholder'=>'请输入角色名称']])->textInput(['maxlength' => true]) ?>

					<?php $model->role_status=1; echo $form->field($model,'role_status')->radioList(['1'=>'开启','0'=>'禁用'],['separator'=>" ","style"=>"padding-top:6px;"]); ?> 

					<?= $form->field($model, 'role_remark',['inputOptions'=>['placeholder'=>'请输入角色信息名称']])->textInput(['maxlength' => true]) ?>
				</div>
			</div>
			<div class="modal-footer">	
				<button type="button" class="btn btn-link" data-dismiss="modal" tabindex="6">取消</button>
				<?= Html::submitButton('保存', ['class' => 'btn btn-success','id'=>'category-create-btn']) ?>
			</div>
			<?php ActiveForm::end(); ?>
		</div>
	</div>
</div>
<div id="RoleUpdate" class="modal fade"></div>
<div id="Auth" class="modal fade"></div>
<div id="RoleInfo" class="modal fade"></div>
<script type="text/javascript">
	//修改
	$(".Save").click(function(){
		var role_id = $(this).attr('type');
		$.ajax({
			type: "POST",
			url: "<?php echo Url::to(['user/roleupdate']);?>",
			data: "role_id="+role_id+"&type=list",
			dataType: 'json',
			success: function(msg){
				if (msg.code == 200)
				{
					$("#RoleUpdate").html(msg.data);
				}else if(msg.code == 403)
				{
					$("#RoleUpdate").html('<div class="modal-dialog " style="width:170px;"><div class="modal-content"><div class="modal-header"><button aria-hidden="true" data-dismiss="modal" class="close" type="button">×</button><h4 class="modal-title"><font color="red">'+msg.msg+'</font></h4></div></div></div>');
				}
			}
		});
	})
	//删除
	$(".Delete").click(function(){
		if(confirm("您确定要删除这条记录？")){
			var role_id=$(this).attr("type");
			$.ajax({
				type: "POST",
				url: "<?php echo Url::to(['user/roledelete']);?>",
				data: "role_id="+role_id,
				dataType: 'json',
				success: function(msg){
					if (msg.code == 200) {
						history.go(0)
					}else if(msg.code == 400)
					{
						alert('删除失败')
					}else if(msg.code == 403) 
					{
						alert('暂无权限')
					}
				}
			});
		}
	})
	//点击禁用
	$(document).on("click",".Off",function(){
		var id = $(this).attr('type');
		var self = $(this)
		$.ajax({
			type: "POST",
			url: "<?php echo Url::to(['user/rolestatus']);?>",
			data: "id="+id+"&status=0",
			dataType: 'json',
			success: function(msg){
				if (msg.code == 200) {
					self.replaceWith('<a href="javascript:;"  class="promote-user On" type="'+id+'">启用</a>')
					$("#"+id).html('<label class="label label-danger">禁用</label>')
				}else if(msg.code == 400)
				{
					alert("修改失败")
				}else if(msg.code == 403) 
				{
					alert('暂无权限')
				}
			}
		});
	})
	//点击启用
	$(document).on("click",".On",function(){
		var id = $(this).attr('type');
		var self = $(this)
		$.ajax({
			type: "POST",
			url: "<?php echo Url::to(['user/rolestatus']);?>",
			data: "id="+id+"&status=1",
			dataType: 'json',
			success: function(msg){
				if (msg.code == 200) {
					self.replaceWith('<a href="javascript:;"  class="promote-user Off" type="'+id+'">禁用</a>')
					$("#"+id).html('<label class="label label-primary">开启</label>')
				}else if(msg.code == 400)
				{
					alert("修改失败")
				}else if(msg.code == 403) 
				{
					alert('暂无权限')
				}
			}
		});
	})
	//分配权限
	$(document).on("click",".Auth",function(){
		var role_id = $(this).attr('type');
		$.ajax({
			type: "POST",
			url: "<?php echo Url::to(['user/authallot']);?>",
			data: "role_id="+role_id,
			dataType: 'json',
			success: function(msg){
				if (msg.code == 200)
				{
					$("#Auth").html(msg.data);
				}else if(msg.code == 403)
				{
					$("#Auth").html('<div class="modal-dialog " style="width:170px;"><div class="modal-content"><div class="modal-header"><button aria-hidden="true" data-dismiss="modal" class="close" type="button">×</button><h4 class="modal-title"><font color="red">'+msg.msg+'</font></h4></div></div></div>');
				}
			}
		});
	})
	//角色详情
	
	$(document).on("click",".RoleInfo",function(){
		var role_id = $(this).attr('type');
		var self = $(this)
		$.ajax({
			type: "POST",
			url: "<?php echo Url::to(['user/roleinfo']);?>",
			data: "role_id="+role_id,
			dataType: 'json',
			success: function(msg){
				if (msg.code == 200)
				{
					$("#RoleInfo").html(msg.data);
				}else if(msg.code == 403)
				{
					$("#RoleInfo").html('<div class="modal-dialog " style="width:170px;"><div class="modal-content"><div class="modal-header"><button aria-hidden="true" data-dismiss="modal" class="close" type="button">×</button><h4 class="modal-title"><font color="red">'+msg.msg+'</font></h4></div></div></div>');
				}
			}
		});
	})
</script>